package cn.homework.web.servlet;

import cn.homework.domain.Orders;
import cn.homework.service.OrdersService;
import cn.homework.service.impl.OrdersServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/ordersServlet")
public class OrdersServlet extends HttpServlet {
    private OrdersService service = new OrdersServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
        switch (demand) {
            case "pay": {
                addOrder(request, response);
                break;
            }
            case "show": {
                ordersShow(request, response);
                break;
            }
            case "reveal": {
                ordersReveal(request, response);
                break;
            }
            case "delete": {
                delOrder(request, response);
                break;
            }
            case "query": {
                findOrdersByCondition(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 添加订单到数据库
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("user");
        Map<String, String[]> map = request.getParameterMap();
        Orders orders = new Orders();
        try {
            BeanUtils.populate(orders, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.addOrderToDatabase(orders, name);
        ordersShow(request, response);
    }

    /**
     * 显示当前用户所有订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void ordersShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("user");
        List<Orders> ordersAllList = service.findAllOrders(name);
        HttpSession session = request.getSession();
        session.setAttribute("ordersAllList", ordersAllList);
        response.sendRedirect(request.getContextPath() + "/CommodityOrderDetails.jsp");
    }

    /**
     * 展示所有订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void ordersReveal(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Orders> ordersAllList = service.findAllOrdersReveal();
        HttpSession session = request.getSession();
        session.setAttribute("ordersAllList", ordersAllList);
        response.sendRedirect(request.getContextPath() + "/BackstageOrderShow.jsp");
    }

    /**
     * 根据id删除订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void delOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.delOrderById(id);
        ordersReveal(request, response);
    }

    /**
     * 根据条件查询订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void findOrdersByCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<Orders> ordersList = service.findOrderByCondition(map);
        HttpSession session = request.getSession();
        session.setAttribute("ordersList", ordersList);
        response.sendRedirect(request.getContextPath() + "/BackstageOrderQuery.jsp");
    }
}
